package cn.edu.lcu.cs.architecture.event.demo;

import java.time.Instant;
import java.util.Random;

import static cn.edu.lcu.cs.architecture.event.demo.Event.Type.KEY;

/**
 * 键盘事件源
 */
public class Key extends EventSource {

    public Key(EventDispatcher dispather) {
        super(dispather);
    }

    /**
     * @return 随机生成的大写字母
     */
    private char generateKeyCode() {
        Random random = new Random();
        return (char) (random.nextInt(26) + 0x41);
    }

    @Override
    public void run() {
        while (true) {
            // 模拟产生一次事件，并添加到事件分发器的队列中
            dispather.enQueue(new Event(KEY, "【" + generateKeyCode() + "】\t" + Instant.now()));
            sleep(); // 随机休眠若干毫秒
        }
    }
}
